package com.googlecode.jerato.library.store;

import com.googlecode.jerato.core.FunctionTransfer;
import com.googlecode.jerato.core.store.StoreTransaction;
import com.googlecode.jerato.core.store.StoreTransfer;
import com.googlecode.jerato.library.StringHashMap;

public class StoreTransferImpl extends StringHashMap implements StoreTransfer{
	protected static final long serialVersionUID = StoreTransferImpl.class.hashCode();

	protected FunctionTransfer _parent;
	protected StoreTransaction _defaultTransaction;
	public StoreTransferImpl(){
	}
	public StoreTransferImpl(FunctionTransfer trans){
		_parent = trans;
	}

	public void flushParent(){
		if(_parent != null){
			_parent.clear();
			_parent.putAll(this);
			if(_parent instanceof StoreTransfer){
				((StoreTransfer)_parent).flushParent();
			}
		}
	}
	public StoreTransaction getTransaction(){
		if(_defaultTransaction != null){
			return _defaultTransaction;
		}
		_defaultTransaction = StoreTransactionService.getInstance().createTransaction();
		return _defaultTransaction;
	}

	public void destroy(){
		if(_defaultTransaction != null){
			_defaultTransaction.end();
		}
	}
	public boolean existsParent() {
		return _parent != null;
	}
	public FunctionTransfer getParent() {
		return _parent;
	}
}
